package com.admin.service.impl;

import com.admin.domain.*;
import com.admin.mapper.*;
import com.admin.req.declare.AttachmentReq;
import com.admin.service.AttachmentService;
import com.admin.service.OssUtilsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Slf4j
@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Autowired
    private AttachmentMapper attachmentMapper;
    @Autowired
    private OssUtilsService ossUtilsService;


    /**
     * 保存文件 - 返回 主键id
     * @param declareId
     * @param attachmentReqs
     * @return
     */
    @Transactional
    @Override
    public String uploadAttachment(Integer declareId, List<AttachmentReq> attachmentReqs) {
        if (CollectionUtils.isEmpty(attachmentReqs) || attachmentReqs.size() <= 0) {
            return "";
        }
        StringBuffer resultSb = new StringBuffer();
        for (AttachmentReq attachmentReq : attachmentReqs) {
            Attachment attachment = new Attachment();
            attachment.setDeclareId(declareId);
            attachment.setAttachName(attachmentReq.getAttachName());
            attachment.setAttachPath(ossUtilsService.getUpdateUrl(attachmentReq.getAttachPath()));
            attachmentMapper.insertSelective(attachment);
            resultSb.append(attachment.getId()).append(",");
        }
        return resultSb.toString();
    }

    /**
     * 根据附件id 查询附件信息
     * @param attachmentIds
     * @return
     */
    @Override
    public List<AttachmentReq> getAttachments(String attachmentIds) {
        if (StringUtils.isEmpty(attachmentIds)) {
            return null;
        }
        String[] strArray = attachmentIds.split(",");
        if (null == strArray || strArray.length < 0) {
            return null;
        }
        List<Integer> attachIds = new ArrayList<>();
        for (String s : strArray) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            attachIds.add(Integer.parseInt(s));
        }
        if (CollectionUtils.isEmpty(attachIds) || attachIds.size() <= 0) {
            return null;
        }

        List<Attachment> attachments = attachmentMapper.findByIds(attachIds);
        if (CollectionUtils.isEmpty(attachments)) {
            return null;
        }

        List<AttachmentReq> results = new ArrayList<>();
        for (Attachment attachment : attachments) {
            AttachmentReq req = new AttachmentReq();
            req.setAttachName(attachment.getAttachName());
            req.setAttachPath(ossUtilsService.getSignUrl(attachment.getAttachPath()));
            results.add(req);
        }
        return results;
    }

    @Override
    public List<Attachment> findListByDeclareIds(List<Integer> declareIdList) {
        List<Attachment> attachments = attachmentMapper.findListByDeclareIds(declareIdList);
        if (!CollectionUtils.isEmpty(attachments)) {
            for (Attachment attachment : attachments) {
                attachment.setAttachPath(ossUtilsService.getSignUrl(attachment.getAttachPath()));
            }
        }
        return attachments;
    }

    @Override
    public int deleteByDeclareId(Integer declareId) {
        return attachmentMapper.deleteByDeclareId(declareId);
    }
}

